<?php
require_once "../load.php";
error_reporting(0);
$action = $_POST["action"];
unset($_POST["action"]);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}
define("AVATAR_DIR", "user-images");
define("AVATAR_PATH", UPLOAD_PATH . AVATAR_DIR . "/");

switch ($action) {
    case 'edit_personal_info' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        $data["phones"] = trim(implode(" ", $_POST["info_phones"]));
        $data["emails"] = trim(implode(" ", $_POST["info_emails"]));
        $owl_personal = new Owl_Personal();
        $old_personal = $owl_personal->get_personal();
        if ($owl_personal->update_personals($data)) {
            $owl_ulang = new Owl_Ulang();

            $to_update = array('name', 'office', 'position', 'degree', 'address');
            foreach ($to_update as $term) {
                $lang[$_POST['lang']] = trim(Owl_Format::esc_html($_POST["info_$term"]));
                $owl_ulang->update_ulang($old_personal[$term], $lang);
            }

            $data = array(
                "code" => 1,
                "message" => "<div class=\"ajax-success\">" . __("Update personal info successful!") . "</div>"
            );
        }
        else {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("Update personal info error: ") . $owl_personal->get_error() . "</div>"
            );
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'reset-personal-info-form' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_personal_form();
        break;
    case 'reset-personal-info-body' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_personal_info();
        break;
    case 'edit_academics' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");

        //Because we will write many time, we shall check the writability first
        if (!is_writable(DATA_PATH . 'personals.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'personal.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }
        if (!is_writable(DATA_PATH . 'ulangs.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'ulang.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }

        $owl_personal = new Owl_Personal();
        $owl_ulang = new Owl_Ulang();
        $update_academics = array();
        $remain_academics = array();
        $new_academics = array();
        $delete_academics = array();
        $personal_info = $owl_personal->get_personal();
        $old_academics = $personal_info['academics'];

        $max_id = 'Academic 0';
        foreach ($_POST["academics"] as $pos=>$academic) {
            $academic = trim(Owl_Format::esc_html($academic));
            $name = $_POST["academics_hidden"][$pos];
            if ($name != 'new_input') {
                if ($name > $max_id) {
                    $max_id = $name;
                }
                $update_academics[] = array(
                    'id' => $name,
                    'value' => $academic
                );
                $remain_academics[] = $name;
            }
            else {
                if (!empty($academic)) {
                    $new_academics[] = array(
                        'value' => $academic
                    );
            }
            }
        }

        if (!empty($old_academics)) {
            foreach ($old_academics as $old_academic) {
                if (!in_array($old_academic, $remain_academics)) {
                    $delete_academics[] = $old_academic;
                }
            }
        }

        $max_id = explode(' ', $max_id);
        $max_id = $max_id[1] + 1;

        if (!empty ($new_academics)) {
            foreach ($new_academics as $pos => $value) {
                $new_academics[$pos]['id'] = 'Academic ' . $max_id;
                $remain_academics[] = $new_academics[$pos]['id'];
                $max_id++;
            }
        }

        $owl_personal->update_personals(array('academics' => $remain_academics));
        
        foreach ($update_academics as $academic) {
            $trans[$_POST['lang']] = $academic['value'];
            $owl_ulang->update_ulang($academic['id'], $trans);
        }
        
        foreach ($delete_academics as $academic) {
            $owl_ulang->delete_ulang($academic);
        }

        foreach ($new_academics as $academic) {
            $trans[$_POST['lang']] = $academic['value'];
            $trans['id'] = $academic['id'];
            $owl_ulang->insert_ulang($trans);
        }

        $data = array(
            "code" => 1,
            "message" => "<div class=\"ajax-success\">" . __("Update academics record successful!") . "</div>"
        );
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'reset-academics-form' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_academics_form();
        break;
    case 'reset-academics-body' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_academics();
        break;
    case 'edit_interests' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        //Because we will write many time, we shall check the writability first
        if (!is_writable(DATA_PATH . 'personals.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'personal.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }
        if (!is_writable(DATA_PATH . 'ulangs.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'ulang.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }

        $owl_personal = new Owl_Personal();
        $owl_ulang = new Owl_Ulang();
        $update_interests = array();
        $remain_interests = array();
        $new_interests = array();
        $delete_interests = array();
        $personal_info = $owl_personal->get_personal();
        $old_interests = $personal_info['interests'];

        $max_id = 'Interest 0';
        foreach ($_POST["interests"] as $pos=>$interest) {
            $interest = trim(Owl_Format::esc_html($interest));            
            $name = $_POST["interests_hidden"][$pos];
            if ($name != 'new_input') {
                if ($name > $max_id) {
                    $max_id = $name;
                }
                $update_interests[] = array(
                    'id' => $name,
                    'value' => $interest
                );
                $remain_interests[] = $name;
            }
            else {
                if (!empty($interest)) {
                    $new_interests[] = array(
                        'value' => $interest
                    );
                }
            }
        }

        if (!empty($old_interests)) {
            foreach ($old_interests as $old_interest) {
                if (!in_array($old_interest, $remain_interests)) {
                    $delete_interests[] = $old_interest;
                }
            }
        }

        $max_id = explode(' ', $max_id);
        $max_id = $max_id[1] + 1;

        if (!empty ($new_interests)) {
            foreach ($new_interests as $pos => $value) {
                $new_interests[$pos]['id'] = 'Interest ' . $max_id;
                $remain_interests[] = $new_interests[$pos]['id'];
                $max_id++;
            }
        }

        $owl_personal->update_personals(array('interests' => $remain_interests));

        foreach ($update_interests as $interest) {
            $trans[$_POST['lang']] = $interest['value'];
            $owl_ulang->update_ulang($interest['id'], $trans);
        }

        foreach ($delete_interests as $interest) {
            $owl_ulang->delete_ulang($interest);
        }

        foreach ($new_interests as $interest) {
            $trans[$_POST['lang']] = $interest['value'];
            $trans['id'] = $interest['id'];
            $owl_ulang->insert_ulang($trans);
        }

        $data = array(
            "code" => 1,
            "message" => "<div class=\"ajax-success\">" . __("Update interests successful!") . "</div>"
        );
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'reset-interests-form' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_interests_form();
        break;
    case 'reset-interests-body' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_interests();
        break;
    case 'edit_certifications' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        //Because we will write many time, we shall check the writability first
        if (!is_writable(DATA_PATH . 'personals.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'personal.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }
        if (!is_writable(DATA_PATH . 'ulangs.xml')) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . sprintf(__("Can not save file %s"), 'ulang.xml') . "</div>"
            );
            echo Owl_Functions::json_encode($data);
            die();
        }

        $owl_personal = new Owl_Personal();
        $owl_ulang = new Owl_Ulang();
        $update_certifications = array();
        $remain_certifications = array();
        $new_certifications = array();
        $delete_certifications = array();
        $personal_info = $owl_personal->get_personal();
        $old_certifications = $personal_info['certifications'];

        $max_id = 'Certification 0';
        foreach ($_POST["certifications"] as $pos=>$certification) {
            $certification = trim(Owl_Format::esc_html($certification));            
            $name = $_POST["certifications_hidden"][$pos];
            if ($name != 'new_input') {
                if ($name > $max_id) {
                    $max_id = $name;
                }
                $update_certifications[] = array(
                    'id' => $name,
                    'value' => $certification
                );
                $remain_certifications[] = $name;
            }
            else {
                if (!empty($certification)) {
                    $new_certifications[] = array(
                        'value' => $certification
                    );
                }
            }
        }

        if (!empty($old_certifications)) {
            foreach ($old_certifications as $old_certification) {
                if (!in_array($old_certification, $remain_certifications)) {
                    $delete_certifications[] = $old_certification;
                }
            }
        }

        $max_id = explode(' ', $max_id);
        $max_id = $max_id[1] + 1;

        if (!empty ($new_certifications)) {
            foreach ($new_certifications as $pos => $value) {
                $new_certifications[$pos]['id'] = 'Certification ' . $max_id;
                $remain_certifications[] = $new_certifications[$pos]['id'];
                $max_id++;
            }
        }

        $owl_personal->update_personals(array('certifications' => $remain_certifications));

        foreach ($update_certifications as $certification) {
            $trans[$_POST['lang']] = $certification['value'];
            $owl_ulang->update_ulang($certification['id'], $trans);
        }

        foreach ($delete_certifications as $certification) {
            $owl_ulang->delete_ulang($certification);
        }

        foreach ($new_certifications as $certification) {
            $trans[$_POST['lang']] = $certification['value'];
            $trans['id'] = $certification['id'];
            $owl_ulang->insert_ulang($trans);
        }

        $data = array(
            "code" => 1,
            "message" => "<div class=\"ajax-success\">" . __("Update certifications successful!") . "</div>"
        );
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'reset-certifications-form' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_certifications_form();
        break;
    case 'reset-certifications-body' :
        $owl_personal = new Owl_Personal();
        $owl_personal->print_certifications();
        break;
    case 'edit_avatar' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        $avatar_data = $_FILES["avatar"];
        if ($avatar_data["error"] == 0 && is_uploaded_file($avatar_data["tmp_name"])) {
            $avatar_file_info = pathinfo($avatar_data['name']);

            //Check file type
            if (!Owl_Functions::file_upload_is_image($avatar_data)) {
                echo '<div class="ajax-error">';
                echo __("Uploaded file is not an image, please upload following file format: JPG, GIF or PNG");
                echo '</div>';
                die();
            }

            $now = Owl_Date_Time::current_time('timestamp');
            $avatar_file_name = "$now." . $avatar_file_info["extension"];
            $avatar_path = AVATAR_PATH . $avatar_file_name;

            //Move image to pages folder
            if (!@move_uploaded_file($avatar_data["tmp_name"], $avatar_path)) {
                echo '<div class="ajax-error">';
                echo __("Error while uploading file, your data is not affected by this error.") . "<br />";
                echo sprintf(__("Cannot create file %s in folder %s"), $avatar_file_name, SITE_URL . UPLOAD_DIR . "/" . AVATAR_DIR);
                echo '</div>';
                die();
            }

            //Update record
            $owl_personal = new Owl_Personal();
            $personal_info = $owl_personal->get_personal();
            if (!$owl_personal->update_personals(array("avatar" => UPLOAD_DIR . "/" . AVATAR_DIR . "/" . $avatar_file_name))) {
                echo '<div class="ajax-error">';
                echo __("Error while update data file, your data is not affected by this error.") . "<br />";
                echo $owl_personal->get_error();
                echo '</div>';

                @unlink($avatar_path);
                die();
            }

            //Remove old avatar file
            @unlink(APS_PATH . $personal_info["avatar"]);

            echo 1;
        }
        else {
            echo '<div class="ajax-error">';
            echo __("Error while uploading file, your data is not affected by this error.") . "<br />";
            echo Owl_Functions::get_upload_error($avatar_data["error"]);
            echo '</div>';
            die();
        }
        break;
    case 'reset_avatar_body' :
        $owl_personal = new Owl_Personal();
        $personal_info = $owl_personal->get_personal();
        echo Owl_Html::make_it_image(Owl_Link::site_url(false) . $personal_info["avatar"], __("Avatar"), 300);
        break;
    case 'reset_avatar_form' :
        $owl_personal = new Owl_Personal();
        $personal_info = $owl_personal->get_personal();
        Owl_Form::print_file_image(array(
            "name" => "avatar",
            "value" => Owl_Link::site_url(false) . $personal_info["avatar"],
            "width" => 300,
            'description' => __("Upload your avatar here. The image size should be 300px width.")
        ));
        break;
    case 'input_lang_change' :
        $owl_personal = new Owl_Personal();
        $translated = $owl_personal->get_personal_with_lang($_POST["lang"]);
        if ($translated == false) {
            $data = array(
                "code" => 0
            );
        }
        else {
            $data = array(
                "code" => 1,
                "name" => $translated[0],
                "office" => $translated[1],
                "position" => $translated[2],
                "degree" => $translated[3],
                "address" => $translated[4]
            );
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'input_lang_change_special' :
        $owl_personal = new Owl_Personal();
        $data = $owl_personal->get_additional_info_with_lang($_POST['type'], $_POST['lang']);
        if ($data == false) {
            $data['code'] = 0;
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    default:
        die();
}

die();